package controller

import (
	"net/http"

	"soma-server/common"
	systemService "soma-server/service/system"
	"soma-server/vo"
	r "soma-server/response"
	"soma-server/pkg/e"

	"github.com/gin-gonic/gin"
	"github.com/go-playground/validator/v10"
)

type IOperationLogController interface {
	GetOperationLogs(c *gin.Context)             // 获取操作日志列表
	BatchDeleteOperationLogByIds(c *gin.Context) //批量删除操作日志
}

type OperationLogController struct {
	operationLogService systemService.IOperationLogService
}

func NewOperationLogController() IOperationLogController {
	operationLogService := systemService.NewOperationLogService()
	operationLogController := OperationLogController{operationLogService: operationLogService}
	return operationLogController
}

// 获取操作日志列表
func (oc OperationLogController) GetOperationLogs(c *gin.Context) {
	var req vo.OperationLogListRequest
	// 绑定参数
	if err := c.ShouldBind(&req); err != nil {
		common.Log.Errorln(err.Error())
		r.Response(c, http.StatusBadRequest, e.BINDING_PARAMS, nil, false)
		return
	}
	// 参数校验
	if err := common.Validate.Struct(&req); err != nil {
		errStr := err.(validator.ValidationErrors)[0].Translate(common.Trans)
		common.Log.Errorln(errStr)
		r.Response(c, http.StatusBadRequest, e.INVALID_PARAMS, nil, false)
		return
	}
	// 获取
	logs, total, err := oc.operationLogService.GetOperationLogs(&req)
	if err != nil {
		common.Log.Errorln("获取操作日志列表失败")
		r.Response(c, http.StatusBadRequest, e.ERROR_GET_LOG_LIST, nil, false)
		return
	}
	
	r.Response(c, http.StatusOK, e.SUCCESS_GET_LOG_LIST, gin.H{
		"list":    logs,
		"total":   total,
	}, true)
}

// 批量删除操作日志
func (oc OperationLogController) BatchDeleteOperationLogByIds(c *gin.Context) {
	var req vo.DeleteOperationLogRequest
	// 参数绑定
	if err := c.ShouldBind(&req); err != nil {
		common.Log.Errorln(err.Error())
		r.Response(c, http.StatusBadRequest, e.BINDING_PARAMS, nil, false)
		return
	}
	// 参数校验
	if err := common.Validate.Struct(&req); err != nil {
		errStr := err.(validator.ValidationErrors)[0].Translate(common.Trans)
		common.Log.Errorln(errStr)
		r.Response(c, http.StatusBadRequest, e.INVALID_PARAMS, nil, false)
		return
	}

	// 删除接口
	err := oc.operationLogService.BatchDeleteOperationLogByIds(req.OperationLogIds)
	if err != nil {
		common.Log.Errorln("删除日志失败")
		r.Response(c, http.StatusBadRequest, e.ERROR_DELETE_LOG, nil, false)
		return
	}

	common.Log.Errorln("删除日志成功")
	r.Response(c, http.StatusOK, e.SUCCESS_DELETE_LOG, nil, true)
}
